package team.seekers.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.skill.entity.dao.Chapter;
import team.seekers.skill.entity.dao.Section;
import team.seekers.skill.entity.dao.Skill;
import team.seekers.skill.entity.dao.UserChapter;
import team.seekers.skill.entity.vo.admin.add.AdminChapterAddVO;
import team.seekers.skill.entity.vo.admin.add.AdminSectionAddVO;
import team.seekers.skill.entity.vo.admin.update.AdminChapterUpdateVO;
import team.seekers.skill.entity.vo.admin.update.AdminSectionUpdateVO;
import team.seekers.skill.entity.vo.fore.list.ChapterListVO;
import team.seekers.skill.mapper.ChapterMapper;
import team.seekers.skill.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.skill.service.SectionService;
import team.seekers.skill.service.SkillService;
import team.seekers.skill.service.UserChapterService;
import team.seekers.skill.common.utils.ConstantUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-01-18
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    private boolean result;


    @Autowired
    private SectionService sectionService;
    @Autowired
    private UserChapterService userChapterService;
    @Autowired
    private SkillService skillService;
    @Autowired
    private ChapterService chapterService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean add(AdminChapterAddVO chapterDo) {
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterDo,chapter);
        chapter.setId(null);
        chapter.setSections(chapterDo.getAdminSectionAddVOList().size());
        boolean save = save(chapter);
        if(!save)
            return false;
        List<AdminSectionAddVO> sectionDoList = chapterDo.getAdminSectionAddVOList();
        for (AdminSectionAddVO sectionDo : sectionDoList) {
            Section section = new Section();
            BeanUtils.copyProperties(sectionDo,section);
            section.setId(null);
            section.setSkillId(chapterDo.getSkillId());
            section.setChapterId(chapter.getId());
            save = sectionService.save(section);
            if(!save)
                return false;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean delete(String chapterId){
        // 删除章的学习记录
        result = userChapterService.removeByChapterId(chapterId);
        if(!result){
            return false;
        }
        // 删除所有节
        result = sectionService.removeByChapterId(chapterId);
        if(!result){
            return false;
        }
        // 删除章
        result = removeById(chapterId);
        return result;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean removeBySkillId(String skillId){
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("skill_id",skillId);
        List<Chapter> list = list(wrapper);
        for (Chapter chapter : list) {
            result = delete(chapter.getId());
            if (!result){
                return false;
            }
        }
        boolean remove = remove(wrapper);
        return remove;
    }

    @Override
    public List<AdminChapterUpdateVO> listAdminChapterUpdateVOBySkillId(String skillId){
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("skill_id",skillId);
        wrapper.orderByAsc("sequence");
        List<Chapter> chapterList = list(wrapper);
        List<AdminChapterUpdateVO> adminChapterUpdateVOList = new ArrayList<>();
        for (Chapter chapter : chapterList) {
            AdminChapterUpdateVO adminChapterUpdateVO = new AdminChapterUpdateVO();
            BeanUtils.copyProperties(chapter,adminChapterUpdateVO);
            List<AdminSectionUpdateVO> adminSectionUpdateVOList = sectionService.listAdminSectionUpdateVOByChapterId(chapter.getId());
            adminChapterUpdateVO.setAdminSectionUpdateVOList(adminSectionUpdateVOList);
            adminChapterUpdateVOList.add(adminChapterUpdateVO);
        }
        return adminChapterUpdateVOList;
    }

    @Override
    public List<ChapterListVO> listChapterVOBySkillId(String userId, String skillId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("skill_id",skillId);
        wrapper.orderByAsc("sequence");
        List<Chapter> chapterList = list(wrapper);
        if(chapterList == null){
            return null;
        }
        List<ChapterListVO> chapterListVoList = new ArrayList<>();
        for (Chapter chapter : chapterList) {
            ChapterListVO chapterListVo = new ChapterListVO();
            BeanUtils.copyProperties(chapter, chapterListVo);
            Integer status = userChapterService.getStatus(userId, chapter.getId());
            chapterListVo.setStatus(status);
            chapterListVoList.add(chapterListVo);
        }
        return chapterListVoList;
    }
    // 开始学习某一章
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean startLearn(String chapterId, String userId) {
        Chapter chapter = chapterService.getById(chapterId);
        UserChapter userChapter = new UserChapter();
        userChapter.setStatus(ConstantUtils.START_LEARN);
        userChapter.setChapterId(chapterId);
        userChapter.setUserId(userId);
        userChapter.setSkillId(chapter.getSkillId());
        boolean save = userChapterService.save(userChapter);
        return save;
    }

    // 结束某一章的学习
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean endLearn(String chapterId, String userId) {
        // 判断是不是最后一章，如果是则结束本技能的学习
        Chapter chapter = getById(chapterId);
        QueryWrapper<UserChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("skill_id",chapter.getSkillId());
        int count = userChapterService.count(wrapper);
        Skill skill = skillService.getById(chapter.getSkillId());
        if(count == skill.getChapters()){
            boolean endLearn = skillService.endLearn(skill.getId(), userId);
            if(!endLearn){
                return false;
            }
        }
        QueryWrapper<UserChapter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",userId);
        wrapper1.eq("chapter_id",chapterId);
        UserChapter userChapter = userChapterService.getOne(wrapper1);
        userChapter.setStatus(2);
        boolean update = userChapterService.updateById(userChapter);
        if (!update)
            return false;
        return true;
    }

}
